﻿using Ellanet.Annotations;
using Ellanet.Classes;
using Ellanet.Wrappers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;

namespace Ellanet.ViewModels
{
    internal class TestWindowViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public event RequestViewCloseHandler RequestViewClose;

        private Sum _sum;
        private readonly List<Sum> _sums;
        private readonly TimeSpan _interval;

        public delegate void RequestViewCloseHandler();

        public Sum Sum
        {
            get { return _sum; }
            set
            {
                _sum = value;
                OnPropertyChanged();
            }
        }

        public TestWindowViewModel(List<Sum> sums, TimeSpan interval)
        {
            _sums = sums;
            _interval = interval;
            ThreadPool.QueueUserWorkItem(StartTest);
        }

        private void StartTest(object stateInfo)
        {
            foreach (var sum in _sums)
            {
                Sum = sum;
                SpeechWrapper.Say(String.Format("{0} times {1}", sum.Multiplicand, sum.Multiplier), true);
                Thread.Sleep(_interval);
                Sum = null;
            }

            //SpeechWrapper.SayAsync("Well done. Now check your answers.", false);
            OnRequestViewClose();
        }

        protected void OnRequestViewClose()
        {
            RequestViewClose?.Invoke();
        }

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
